﻿Imports System.Windows.Controls.Primitives
Imports System.ComponentModel

Public Class EnvelopePointLevel
    Inherits Thumb

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(EnvelopePointLevel), New FrameworkPropertyMetadata(GetType(EnvelopePointLevel)))
    End Sub



    Public Property UpperLength As GridLength
        Get
            Return DirectCast(GetValue(UpperLengthProperty), GridLength)
        End Get
        Set(ByVal value As GridLength)
            SetValue(UpperLengthProperty, value)
        End Set
    End Property

    Public Shared ReadOnly UpperLengthProperty As DependencyProperty = DependencyProperty.Register( _
        "UpperLength", GetType(GridLength), GetType(EnvelopePointLevel), _
        New FrameworkPropertyMetadata(New GridLength(1, GridUnitType.Star)))



    Public Property LowerLength As GridLength
        Get
            Return DirectCast(GetValue(LowerLengthProperty), GridLength)
        End Get
        Set(ByVal value As GridLength)
            SetValue(LowerLengthProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LowerLengthProperty As DependencyProperty = DependencyProperty.Register( _
        "LowerLength", GetType(GridLength), GetType(EnvelopePointLevel), _
        New FrameworkPropertyMetadata(New GridLength(0, GridUnitType.Star)))



    <Category("FMMatrix")> _
    Public Property Value As Double
        Get
            Return DirectCast(GetValue(ValueProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(ValueProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register( _
        "Value", GetType(Double), GetType(EnvelopePointLevel), _
        New FrameworkPropertyMetadata(0.0, _
            Sub(d, e) DirectCast(d, EnvelopePointLevel).OnValueChanged( _
                DirectCast(e.OldValue, Double), DirectCast(e.NewValue, Double))), _
        Function(o) TypeOf o Is Double AndAlso ValidateValue(DirectCast(o, Double)))

    Private Shared Function ValidateValue(d As Double) As Boolean
        Return Not Double.IsNaN(d) AndAlso Not Double.IsInfinity(d) AndAlso d >= 0 And d <= 1
    End Function

    Protected Overridable Sub OnValueChanged(oldValue As Double, newValue As Double)
        UpperLength = New GridLength(1 - newValue, GridUnitType.Star)
        LowerLength = New GridLength(newValue - 0, GridUnitType.Star)
    End Sub



    Private origValue As Double

    Private Sub EnvelopePhaseDuration_DragStarted(sender As Object, e As DragStartedEventArgs) Handles Me.DragStarted
        origValue = Value
    End Sub

    Private Sub EnvelopePhaseDuration_DragDelta(sender As Object, e As DragDeltaEventArgs) Handles Me.DragDelta
        Dim _value = -e.VerticalChange / ActualHeight + origValue
        If _value < 0 Then _value = 0
        If _value > 1 Then _value = 1
        Value = _value
    End Sub

End Class
